#coding:utf-8
from bs4 import BeautifulSoup
import os, sys
import string
import re
import time
import pymysql as mysql

import configparser

config=configparser.ConfigParser()	
with open("config.ini","r") as cfgfile:
	config.readfp(cfgfile)
	
	ROOT = config.get( "fileroot", "root" )
	



def insert_sql(sqlList):
	conn = mysql.connect( host = '192.168.0.91', port = 3306, user = 'root', passwd = 'vipdatacenter', db = 'botu',charset='utf8mb4',)
	cur = conn.cursor()
	for sql in sqlList:
		cur.execute(sql)
	conn.commit()
	conn.close()
def  get_data(parent, filename):
	rawid = filename[:-5]
	file_root = os.path.join(parent, filename)
	f = open(file_root,'r',encoding = "utf-8")
	htmlText = f.read()
	soup = BeautifulSoup(htmlText,'lxml')
	div = soup.find('div', id ='photo')
	img = div.find('img')
	url = img.get('src')
	sql =  "insert ignore into cover (rawid,url) values ('%s','%s')" %(rawid,url) 
	return sql
if __name__=="__main__":
	Htmlroot = os.path.join(ROOT, time.strftime('%Y%m%d',time.localtime(time.time())),"html")
	sqlList = []
	index = 0
	for parent, dirnames, filenames in os.walk(Htmlroot):
		index += 1
		for filename in filenames:
			if not filename.endswith('.html'):
				continue
			sql = get_data(parent, filename)
			sqlList.append(sql)
		print(index)
		insert_sql(sqlList)
		sqlList = []
			